package cn.easy.service.impl;

import cn.easy.api.ActionService;
import cn.easy.common.pojo.ActionType;
import cn.easy.common.pojo.EasyResult;
import cn.easy.mapper.*;
import cn.easy.pojo.*;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 动作业务层
 *
 * @author 谢子豪
 * @time 2017/10/6
 * @description
 */
@Service
public class ActionServiceImpl implements ActionService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ActionMapper actionMapper;


    /**
     * 新增一个动作 需要身份验证
     *
     * @param action
     * @param userKey
     * @throws Exception
     */
    @Override
    public void insertAction(Action action, String userKey) throws Exception {

        /*--------------通过用户key找到用户------------*/
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserKeyEqualTo(userKey);
        List<User> list = userMapper.selectByExample(userExample);
        if (list.size() == 0) throw new Exception("找不到key所对应的用户");
        /*--------------为action设置所属用户------------*/
        User user = list.get(0);
        action.setUserId(user.getUserId());
        /*--------------新增动作------------*/
        actionMapper.insert(action);
    }

    /**
     * 删除一个动作
     *
     * @param actionId
     * @param userKey
     * @throws Exception
     */
    @Override
    public void deleteAction(int actionId, String userKey) throws Exception {
        /*--------------拿到动作对应的用户id------------*/
        Action action = actionMapper.selectByPrimaryKey(actionId);
        if (action == null) throw new Exception("找不到对应的动作");
        Integer actionUserId = action.getUserId();
        /*--------------身份验证------------*/
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserKeyEqualTo(userKey);
        List<User> users = userMapper.selectByExample(userExample);
        if (users.size() == 0) throw new Exception("找不到key对应的用户");
        if (actionUserId != users.get(0).getUserId()) throw new Exception("身份验证失败");
        /*--------------执行删除操作------------*/
        actionMapper.deleteByPrimaryKey(actionId);
    }

    /**
     * 更新一个动作
     *
     * @param action
     * @param userKey
     * @throws Exception
     */
    @Override
    public void updateAction(Action action, String userKey) throws Exception {

        Action oldAction = actionMapper.selectByPrimaryKey(action.getActionId());
        if(oldAction == null) throw new Exception("找不到动作");
        /*--------------身份验证------------*/
        User user = userMapper.selectByUserKey(userKey);
        if (user == null) throw new Exception("找不到对应的用户");
        if(oldAction.getUserId() != user.getUserId()) throw new Exception("身份验证失败");

        /*--------------构造更新数据------------*/


        if (action.getActionDesc() != null) oldAction.setActionDesc(action.getActionDesc());//内容
        if (action.getActionType() != null) oldAction.setActionType(action.getActionType());//动作类型
        /*--------------执行更新操作------------*/
        actionMapper.updateByPrimaryKey(oldAction);
    }

    /**
     * 查询用户名下所有的动作
     * @param userKey
     * @return
     * @throws Exception
     */
    public List<Action> selectUserAction(String userKey) throws Exception {
        User user = userMapper.selectByUserKey(userKey);
        if (user == null) throw new Exception("找不到对应的用户");

        /*--------------查询------------*/
        ActionExample actionExample = new ActionExample();
        ActionExample.Criteria criteria = actionExample.createCriteria();
        criteria.andUserIdEqualTo(user.getUserId());
        List<Action> actionList = actionMapper.selectByExample(actionExample);
        return actionList;
    }

    /**
     * 根据动作的id查询动作
     * @param actionId
     * @return
     */
    @Override
    public Action getActionById(int actionId) {
        Action action = actionMapper.selectByPrimaryKey(actionId);
        return action;
    }

}
